﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedemptoristRetreatCenter.DatabaseUtility;

namespace RedemptoristRetreatCenter.Account
{
    /// <summary>
    /// Contains the login functionality for the webpage
    /// </summary>
    public partial class Login : Page
    {
        /// <summary>
        /// Upon page load, if a previous page exists, save the previous page for future reference
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            // -- false when 1st visit; true if user backing up --
            if (!Page.IsPostBack)
                // -- Saves the Previous page url in ViewState --
                ViewState["PreviousPage"] = Request.UrlReferrer;
        }

        /// <summary>
        /// Upon login click, verify validity of username, password, and act correspondingly
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Login_Click(object sender, EventArgs e)
        {
            UserAccountUtility userAccountUtil = new RedemptoristRetreatCenter.DatabaseUtility.UserAccountUtility();

            // -- Verifies that username is valid --
            if (userAccountUtil.userExists(UserName.Text))//Login1.UserName))
            {
                // -- Populates userAccountInfo with the user's info if the username/password are correct --
                UserAccountInfo userAccountInfo = userAccountUtil.validateCredentials(UserName.Text, Password.Text);//Login1.UserName, Login1.Password);

                // -- if the credentials were correct, populate session variables, return to previous page --
                if (userAccountInfo != null)
                {
                    Session["Username"] = UserName.Text;//Login1.UserName;
                    Session["isAdmin"] = userAccountInfo.administrator;
                    FailureText.Text = "You have successfully logged in.";
                    ReturnToPrevPage();
                }
                // -- password does not match username --
                else
                {
                    Session["Username"] = null;
                    Session["isAdmin"] = false;
                    FailureText.Visible = true;
                    FailureText.Text = "Your password was incorrect. Please re-enter your credentials.";
                }
            }
            // -- username does not exist --
            else
            {
                Session["Username"] = null;
                Session["isAdmin"] = false;
                FailureText.Visible = true;
                FailureText.Text = "Your username is invalid.  Please re-enter your credentials.";
            }
        }

        /// <summary>
        /// Once user has logged in, return the user to the previous page they visited within the Redemptorist Retreat Website --
        /// </summary>
        private void ReturnToPrevPage()
        {
            // -- Checking whether user was previously on another page --
            if ((ViewState["PreviousPage"] != null) && !(ViewState["PreviousPage"].ToString().Contains("LogoutConfirmation")))
            {
                // -- Redirect to Previous Page (retrieve PreviousPage url from ViewState) --
                Response.Redirect(ViewState["PreviousPage"].ToString());
            }
        }

    }
}